// 发放红花页面
Page({
  data: {
    students: [],
    selectedStudents: [],
    flowerCount: 1,
    reason: '',
    loading: false,
    submitting: false,
    allSelected: false
  },

  onLoad() {
    console.log('发放红花页面加载');
    this.loadStudents();
  },

  // 加载学生列表
  async loadStudents() {
    this.setData({ loading: true });
    
    try {
      // 模拟数据，实际项目中应该从服务器获取
      const students = [
        { id: 1, name: '张三', avatar: '👦', selected: false },
        { id: 2, name: '李四', avatar: '👧', selected: false },
        { id: 3, name: '王五', avatar: '👦', selected: false },
        { id: 4, name: '赵六', avatar: '👧', selected: false },
        { id: 5, name: '钱七', avatar: '👦', selected: false }
      ];
      
      this.setData({ 
        students: students,
        loading: false 
      });
    } catch (error) {
      console.error('获取学生列表失败:', error);
      this.setData({ loading: false });
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    }
  },

  // 选择学生
  selectStudent(e) {
    const studentId = e.currentTarget.dataset.id;
    const students = this.data.students.map(student => {
      if (student.id === studentId) {
        return { ...student, selected: !student.selected };
      }
      return student;
    });
    
    const selectedStudents = students.filter(student => student.selected);
    const allSelected = students.length > 0 && students.every(student => student.selected);
    
    this.setData({ 
      students: students,
      selectedStudents: selectedStudents,
      allSelected: allSelected
    });
  },

  // 全选/取消全选
  toggleSelectAll() {
    const allSelected = this.data.allSelected;
    const students = this.data.students.map(student => ({
      ...student,
      selected: !allSelected
    }));
    
    const selectedStudents = !allSelected ? students : [];
    
    this.setData({ 
      students: students,
      selectedStudents: selectedStudents,
      allSelected: !allSelected
    });
  },

  // 红花数量变化
  onFlowerCountChange(e) {
    this.setData({ flowerCount: parseInt(e.detail.value) || 1 });
  },

  // 原因输入
  onReasonInput(e) {
    this.setData({ reason: e.detail.value });
  },

  // 提交发放
  async submitGiveFlower() {
    const { selectedStudents, flowerCount, reason } = this.data;
    
    if (selectedStudents.length === 0) {
      wx.showToast({
        title: '请选择学生',
        icon: 'none'
      });
      return;
    }
    
    if (!reason.trim()) {
      wx.showToast({
        title: '请输入发放原因',
        icon: 'none'
      });
      return;
    }
    
    this.setData({ submitting: true });
    
    try {
      // 模拟API调用
      await new Promise(resolve => setTimeout(resolve, 1000));
      
      wx.showToast({
        title: '红花发放成功',
        icon: 'success'
      });
      
      // 返回上一页
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
      
    } catch (error) {
      console.error('发放红花失败:', error);
      wx.showToast({
        title: '发放失败，请重试',
        icon: 'none'
      });
    } finally {
      this.setData({ submitting: false });
    }
  },

  // 快速发放
  quickGiveFlower(e) {
    const student = e.currentTarget.dataset.student;
    if (student) {
      wx.showModal({
        title: '快速发放红花',
        content: `给${student.name}发放1朵红花？`,
        confirmText: '发放',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            wx.showToast({
              title: '红花发放成功',
              icon: 'success'
            });
          }
        }
      });
    }
  }
});




